﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThanhTu.GNC.Media.KQXS.DataAccess.DO;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using ThanhTu.GNC.Media.KQXS.Common;

namespace ThanhTu.GNC.Media.KQXS.DataAccess.DAL
{
    public class ProvinceDAL : BaseDAL
    {
        public ProvinceDAL() : base() { }
        public int SaveProvince(ProvinceDO vProvinceDO)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "PROVINCE_PKG.SAVEPROVINCE";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("IN_PROVINCE_ID", OracleDbType.Varchar2);
                oraParam.Value = vProvinceDO.ProvinceID;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("IN_REGION_ID", OracleDbType.Varchar2);
                oraParam.Value = vProvinceDO.RegionID;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("IN_PRINCE_NAME", OracleDbType.Varchar2);
                oraParam.Value = vProvinceDO.PrinceName;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.Int32);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                int result = ExecuteNoneQuery(oraComm);

                if (!Convert.IsDBNull(oraComm.Parameters["OUTRETURN"]))
                { result = Convert.ToInt32(oraComm.Parameters["OUTRETURN"].Value.ToString()); }

                oraComm.Dispose();
                oraParam = null;

                return result;
            }
        }
        public int delete(string ID_Province)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "PROVINCE_PKG.DELETEPROVINCE";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("IN_PROVINCE_ID", OracleDbType.Varchar2);
                oraParam.Value = ID_Province;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.Int32);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                int result = ExecuteNoneQuery(oraComm);

                if (!Convert.IsDBNull(oraComm.Parameters["OUTRETURN"]))
                { result = Convert.ToInt32(oraComm.Parameters["OUTRETURN"].Value.ToString()); }

                oraComm.Dispose();
                oraParam = null;

                return result;
            }
        }
        public ProvinceDO getbyProvince(string ID_Province)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "PROVINCE_PKG.GETBYPROVINCE";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("IN_PROVINCE_ID", OracleDbType.Varchar2);
                oraParam.Value = ID_Province;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                using (DataSet ds = GetDataSet(oraComm))
                {
                    DataRow dr = null;
                    ProvinceDO vProvinceDO = null;

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                dr = ds.Tables[0].Rows[0];
                                vProvinceDO = new ProvinceDO();
                                if (!Convert.IsDBNull(dr["PROVINCEID"]))
                                { vProvinceDO.RegionID = Convert.ToString(dr["PROVINCEID"]); }

                                if (!Convert.IsDBNull(dr["REGIONID"]))
                                { vProvinceDO.RegionID = Convert.ToString(dr["REGIONID"]); }

                                if (!Convert.IsDBNull(dr["PRINCENAME"]))
                                { vProvinceDO.RegionID = Utils.ToUTF8String((byte[])dr["PRINCENAME"]); }

                                dr = null;
                            }
                        }

                        ds.Dispose();
                    }

                    oraComm.Dispose();
                    oraParam = null;

                    return vProvinceDO;
                }
            }
        }
        public List<ProvinceDO> selectadd()
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "PROVINCE_PKG.GERALLPROVINCENONEPAGE";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                using (DataSet ds = GetDataSet(oraComm))
                {
                    DataTable dt = null;
                    List<ProvinceDO> vProvinceDOCollection = null;
                    DataRow dr = null;
                    ProvinceDO vProvinceDO = null;

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            dt = ds.Tables[0];
                            vProvinceDOCollection = new List<ProvinceDO>();

                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                dr = dt.Rows[i];
                                vProvinceDO = new ProvinceDO();

                                if (!Convert.IsDBNull(dr["PROVINCEID"]))
                                { vProvinceDO.ProvinceID = Convert.ToString(dr["PROVINCEID"]); }

                                if (!Convert.IsDBNull(dr["REGIONID"]))
                                { vProvinceDO.RegionID = Convert.ToString(dr["REGIONID"]); }


                                if (!Convert.IsDBNull(dr["PRINCENAME"]))

                                { vProvinceDO.PrinceName = Utils.ToUTF8String((byte[])dr["PRINCENAME"]); }


                                vProvinceDOCollection.Add(vProvinceDO);
                                dr = null;
                                vProvinceDO = null;
                            }

                            dt.Dispose();
                            dt = null;
                        }

                        ds.Dispose();
                    }
                    oraComm.Dispose();
                    oraParam = null;

                    return vProvinceDOCollection;
                }
            }
        }
        public List<ProvinceDO> selectadd_idvung(string Id_Region)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "Province_PKG.GerAllProvinceNonePage_Region";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("IN_REGION_ID", OracleDbType.Varchar2);
                oraParam.Value = Id_Region;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                using (DataSet ds = GetDataSet(oraComm))
                {
                    DataTable dt = null;
                    List<ProvinceDO> vProvinceDOCollection = null;
                    DataRow dr = null;
                    ProvinceDO vProvinceDO = null;

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            dt = ds.Tables[0];
                            vProvinceDOCollection = new List<ProvinceDO>();

                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                dr = dt.Rows[i];
                                vProvinceDO = new ProvinceDO();

                                if (!Convert.IsDBNull(dr["PROVINCEID"]))
                                { vProvinceDO.ProvinceID = Convert.ToString(dr["PROVINCEID"]); }

                                if (!Convert.IsDBNull(dr["REGIONID"]))
                                { vProvinceDO.RegionID = Convert.ToString(dr["REGIONID"]); }


                                if (!Convert.IsDBNull(dr["PRINCENAME"]))
                                { vProvinceDO.PrinceName = Utils.ToUTF8String((byte[])dr["PRINCENAME"]); }


                                vProvinceDOCollection.Add(vProvinceDO);
                                dr = null;
                                vProvinceDO = null;
                            }

                            dt.Dispose();
                            dt = null;
                        }

                    }
                    return vProvinceDOCollection;
                }
            }
        }
        public DataTable SelectAll_page(int pageIndex, int pageSize, ref int rowCount)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "PROVINCE_PKG.GERALLPROVINCEPAGE";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("INPAGEINDEX", OracleDbType.Int32);
                oraParam.Value = pageIndex;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("INPAGESIZE", OracleDbType.Int32);
                oraParam.Value = pageSize;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTROWCOUNT", OracleDbType.Int32);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);


                using (DataSet ds = GetDataSet(oraComm))
                {
                    DataTable dt = null;

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            dt = ds.Tables[0];

                            if (!Convert.IsDBNull(oraComm.Parameters["OUTROWCOUNT"]))
                            { rowCount = Convert.ToInt32(oraComm.Parameters["OUTROWCOUNT"].Value.ToString()); }
                        }

                        ds.Dispose();
                    }

                    oraComm.Dispose();
                    oraParam = null;

                    return dt;
                }
            }
        }
        public DataTable SelectAll_page_idvung(string id_Region, int pageIndex, int pageSize, ref int rowCount)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "PROVINCE_PKG.GERALLPROVINCEPAGE_REGION";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("IN_REGION_ID", OracleDbType.Varchar2);
                oraParam.Value = id_Region;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("INPAGEINDEX", OracleDbType.Int32);
                oraParam.Value = pageIndex;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("INPAGESIZE", OracleDbType.Int32);
                oraParam.Value = pageSize;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTROWCOUNT", OracleDbType.Int32);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                using (DataSet ds = GetDataSet(oraComm))
                {
                    DataTable dt = null;

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            dt = ds.Tables[0];

                            if (!Convert.IsDBNull(oraComm.Parameters["OUTROWCOUNT"]))
                            { rowCount = Convert.ToInt32(oraComm.Parameters["OUTROWCOUNT"].Value.ToString()); }
                        }

                        ds.Dispose();
                    }

                    oraComm.Dispose();
                    oraParam = null;

                    return dt;
                }
            }
        }
    }
}
